package com.briup.estore.service.impl;

import com.briup.estore.bean.Customer;
import com.briup.estore.bean.CustomerExample;
import com.briup.estore.dao.CustomerMapper;
import com.briup.estore.service.CustomerService;
import com.briup.estore.util.MybatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class CustomerServiceImpl implements CustomerService {

    private CustomerMapper customerMapper;
    {
        SqlSession sqlSession = MybatisSqlSessionFactory.openSession();
        customerMapper = sqlSession.getMapper(CustomerMapper.class);
    }

    @Override
    public Customer login(String username, String password) {
        // 使用mapper做一个条件查询
        CustomerExample example = new CustomerExample();
        example.createCriteria().andNameEqualTo(username).andPasswordEqualTo(password);
        List<Customer> customers = customerMapper.selectByExample(example);
        if(customers == null || customers.size() == 0){
            return null;
        }else {
            return customers.get(0);
        }
    }

    @Override
    public void register(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public void logout(Integer customerId) {

    }
}
